﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Cav;
using Cav.WinForm.BaseClases;
using Nefela.BusinessLogic;
using Nefela.CommonTypes;

namespace Nefela.UI
{
    public partial class fmSettings : DialogFormBase
    {
        public fmSettings()
        {
            InitializeComponent();
        }

        private SettingsBL setsBL;

        private void btOutputFolderSeek_Click(object sender, EventArgs e)
        {
            var sp = Dialogs.FolderBrowser(
                Description: "Каталог для заполненых пакетов докуметов",
                SelectedPath: tbOutputFolder.Text,
                ShowNewFolderButton: true);

            if (!sp.IsNullOrWhiteSpace())
                tbOutputFolder.Text = sp;
        }

        private void fmSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult != System.Windows.Forms.DialogResult.OK && setsBL.ModelPackegeDocs.HasChanged)
            {
                String msg = "Настройки изменены. Сохранить изменения?";
                var rd = Dialogs.QuestionYesNoCancelF(this, msg, "Настройки");
                if (!rd.HasValue)
                {
                    e.Cancel = true;
                    return;
                }

                if (!rd.Value)
                {
                    setsBL.LoadSetting();
                    return;
                }
            }

            setsBL.ModelPackegeDocs.OutputDir = tbOutputFolder.Text;           
            setsBL.ModelPackegeDocs.HasChanged = false;
            setsBL.Save();
        }

        private void fmSettings_Load(object sender, EventArgs e)
        {
            setsBL = (SettingsBL)this.BusinessLogic;

            tbOutputFolder.Text = setsBL.ModelPackegeDocs.OutputDir;

            dgvTempDoc.AutoGenerateColumns = false;
            dgvTempDoc.DataSource = setsBL.ModelPackegeDocs.TemplatesDocs;
            dgvTempDoc.ClearSelection();


            dgvFields.AutoGenerateColumns = false;
            dgvFields.DataSource = setsBL.ModelPackegeDocs.CardCustomer;

            dgvFields.ClearSelection();
        }

        private void btTemplDocAdd_Click(object sender, EventArgs e)
        {
            var adoc = new fmDocAdd();
            adoc.BusinessLogic = setsBL;

            adoc.ShowDialog(this);
        }

        private void dgvTempDoc_CellClick(object sender, EventArgs e)
        {
            btTempDocDel.Enabled =
                btTemplDocEdit.Enabled =
                dgvTempDoc.SelectedRows.Count != 0;
        }

        private void btTemplDocEdit_Click(object sender, EventArgs e)
        {
            var dgvr = dgvTempDoc.SelectedRows.Cast<DataGridViewRow>().FirstOrDefault();

            if (dgvr == null)
                return;

            var td = (TemplateDocumentType)dgvr.DataBoundItem;
            if (td == null)
                return;

            var adoc = new fmDocAdd();
            adoc.BusinessLogic = setsBL;
            adoc.TempDoc = td;

            adoc.ShowDialog(this);
        }

        private void btTempDocDel_Click(object sender, EventArgs e)
        {
            var dgvr = dgvTempDoc.SelectedRows.Cast<DataGridViewRow>().FirstOrDefault();

            if (dgvr == null)
                return;

            var td = (TemplateDocumentType)dgvr.DataBoundItem;
            if (td == null)
                return;

            if (!Dialogs.QuestionOKCancelF(this, String.Format("Удалить шаблон документа {0} ?", td.Name)))
                return;

            setsBL.ModelPackegeDocs.TemplatesDocs.Remove(td);

            dgvTempDoc.ClearSelection();
        }

        private void btFieldAdd_Click(object sender, EventArgs e)
        {
            var riae = new fmFCCAddEdit();
            riae.BusinessLogic = setsBL;

            riae.ShowDialog(this);
        }

        private void btFieldDel_Click(object sender, EventArgs e)
        {
            var dgvr = dgvFields.SelectedRows.Cast<DataGridViewRow>().FirstOrDefault();

            if (dgvr == null)
                return;

            var td = (FieldCardCustomerType)dgvr.DataBoundItem;

            if (!Dialogs.QuestionOKCancelF(this, String.Format("Удалить элемент карточки {0} ?", td.Name)))
                return;

            setsBL.ModelPackegeDocs.CardCustomer.Remove(td);

            dgvFields_CellClick(null, null);
        }

        private void btFieldEdit_Click(object sender, EventArgs e)
        {
            var dgvr = dgvFields.SelectedRows.Cast<DataGridViewRow>().FirstOrDefault();

            if (dgvr == null)
                return;

            var riae = new fmFCCAddEdit();
            riae.BusinessLogic = setsBL;
            riae.FieldItemRow = (FieldCardCustomerType)dgvr.DataBoundItem;

            riae.ShowDialog(this);
        }

        private void dgvFields_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            btFieldDel.Enabled =
                btFieldEdit.Enabled =
                    dgvFields.SelectedRows.Count != 0;
        }

        private void fmSettings_HelpRequested(object sender, HelpEventArgs hlpevent)
        {
            Process.Start("https://goo.gl/gYLsjV");
        }
    }
}
